import requests
import re
import csv

#豆瓣top250 request、re、csv练习demo
url="https://movie.douban.com/top250"

data={
    "start":"0"
}

agent={
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"
}

html=requests.get(url=url,headers=agent)
RegExp1=re.compile(r'<span class="title">(?P<MovieName>.*?)</span>'
                  r'.*?<br>(?P<Year>.*?)&nbsp.*?'
                  r'<span class="rating_num" property="v:average">.*?</span>.*?'
                  r'<span>(?P<Num>.*?)人评价</span>',re.S)
htmlContent=RegExp1.finditer(html.text)
file=open("resource/data.csv",mode="w",encoding="utf-8")
writer=csv.writer(file)
for i in htmlContent:
    print(i.group("MovieName"))
    print(i.group("Year").strip())
    print(i.group("Num"))
    dic=i.groupdict()
    dic['Year']=dic['Year'].strip()
    writer.writerow(dic.values())
print("over！！")
file.close()
html.close()


